home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Tools & Apps / OS⁄Toolbox / QuickDraw / RlePict / macimg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-08  |  6.9 KB  |  253 lines  |  [TEXT/KAHL]

  1. /*
  2.  * This software is copyrighted as noted below.  It may be freely copied,
  3.  * modified, and redistributed, provided that the copyright notice is 
  4.  * preserved on all copies.
  5.  * 
  6.  * There is no warranty or other guarantee of fitness for this software,
  7.  * it is provided solely "as is".  Bug reports or fixes may be sent
  8.  * to the author, who may or may not act on them as he desires.
  9.  *
  10.  * You may not include this software in a program or other software product
  11.  * without supplying the source, or without informing the end-user that the 
  12.  * source is available for no extra charge.
  13.  *
  14.  * If you modify this software, you should include a notice giving the
  15.  * name of the person performing the modification, the date of modification,
  16.  * and the reason for such modification.
  17.  */
  18. /*
  19.  * Routines to manipulate images and Pict files in 32bit QuickDraw.
  20.  *
  21.  * John Peterson, Jan-Feb '91
  22.  *
  23.  * Copyright (c) 1991 John Peterson
  24.  */
  25.  
  26. #include <QDOffscreen.h>
  27. #define srcDither 64    /* Undefined transfer mode... */
  28.  
  29. #include "macstuff.h"
  30. #include "macimg.h"
  31. #include <Stdio.h>
  32. #include <string.h>
  33. #include <pascal.h>
  34.  
  35. static PicHandle thePic;
  36.  
  37. /*******************************************************************
  38.  *                         P I C T U R E   I / O                       *
  39.  *******************************************************************/
  40.  
  41. /*
  42.  * Start recording an image.  Note the call:
  43.  *
  44.  *         CopyBits( (BitMap *) &(thePort->portBits), 
  45.  *                   (BitMap *) &(thePort->portBits), 
  46.  *                   &(thePort->portRect), 
  47.  *                   &(thePort->portRect), srcCopy, NULL );
  48.  * 
  49.  * Records an image of the current port.  "thePort" can also be
  50.  * a WindowPtr.  So to save an image file, do something like:
  51.  *
  52.  *         SetupPict()
  53.  *        // Do Quickdraw drawing commands (like the CopyBits above) here
  54.  *        WritePictFile("Imagefile.PICT");
  55.  *
  56.  * See SavePicture() below...
  57.  */
  58. void
  59. SetupPict()
  60. {
  61.     thePic = OpenPicture( &thePort->portRect );
  62. }
  63.  
  64. /*
  65.  * Write out the picture as a "PICT" file.  
  66.  */
  67. void
  68. WritePictFile(fname)
  69. char * fname;
  70. {
  71.     int i, err;
  72.     Str255 volName;
  73.     int curVolRefNum, refNum;
  74.     long len;
  75.     FInfo ftype;
  76.     unsigned char nulls[512];
  77.     char name[255];
  78.     static char curname = 'A';
  79.     
  80.     ClosePicture();
  81.     HLock( thePic );
  82.     DrawPicture( thePic, &((*thePic)->picFrame) );    /* So it's visible */
  83.     
  84.     for (i = 0; i < 512; i++) nulls[i] = 0;
  85.     
  86.     err = GetVol( volName, &curVolRefNum );
  87.     ToolboxError( "Error writing PICT file, can't get volume", err );
  88.     
  89.     /* If name is NULL, generate MacDraw II pictures for debugging
  90.      * (this was useful for another application - RlePict shouldn't do this!).
  91.      */
  92.     
  93.     if (! fname)
  94.     {
  95.         strcpy( name, "ZZpictX" );
  96.         name[6] = curname++;
  97.         err = Create( CtoPstr( name ), curVolRefNum, 'MDPL', 'PICT' );
  98.     }
  99.     else
  100.     {
  101.         strcpy( name, fname );
  102.         err = Create( CtoPstr( name ), curVolRefNum, 'RAPP', 'PICT' );
  103.         ToolboxError( "Error writing PICT file, can't create file", err );
  104.     }
  105.  
  106.     /*
  107.      * The Apple convention is that the first 512 bytes of a
  108.      * PICT file are ignored (usually NULL's).
  109.      */
  110.     err = FSOpen( name, curVolRefNum, &refNum );
  111.     ToolboxError( "Error writing PICT file, can't open file", err );
  112.     len = 512L;
  113.     err = FSWrite( refNum, &len, nulls );
  114.     ToolboxError( "Error writing PICT file, can't write header", err );
  115.     
  116.     len = GetHandleSize( thePic ); /* Don't use (*thePic)->picSize; it's only 16 bits! */
  117.     err = FSWrite( refNum, &len, *thePic );
  118.     ToolboxError( "Error writing PICT file, can't write data", err );
  119.     err = FSClose( refNum );
  120.     ToolboxError( "Error writing PICT file, can't close file", err );
  121.     
  122.     HUnlock( thePic );
  123.     KillPicture( thePic );
  124. }
  125.  
  126. /*
  127.  * Load the Pict file "name" into a GWorld created by this routine.
  128.  */
  129. GWorldPtr LoadPicture(name)
  130. char * name;
  131. {
  132.     GDHandle saveDevice;
  133.     CGrafPtr savePort;
  134.     OSErr err;
  135.     Str255 volName;
  136.     int curVolRefNum, refNum;
  137.     char buf[512];
  138.     long len;
  139.     PicHandle qdpic;
  140.     GWorldPtr mygw;
  141.  
  142.     err = GetVol( volName, &curVolRefNum );
  143.     ToolboxError( "Error loading PICT file, can't set volume", err );
  144.     err = FSOpen( CtoPstr( name ), curVolRefNum, &refNum );
  145.     ToolboxError( "Error loading PICT file, can't set volume", err );
  146.     err = GetEOF( refNum, &len );
  147.     ToolboxError( "Error loading PICT file, can't get EOF", err );
  148.     if (len <= 512)
  149.     {
  150.         ErrorAlert("Pict file is empty");
  151.         err = FSClose( refNum );
  152.         return NULL;
  153.     }
  154.     err = SetFPos( refNum, fsFromStart, 512L );
  155.     ToolboxError( "Error loading PICT file, can't skip header", err );
  156.     qdpic = (PicHandle) NewHandle( len - 512L );
  157.     if (! qdpic)
  158.         MemoryError( "Error loading PICT file, not enough memory" );
  159.     HLock( qdpic );
  160.     len -= 512L;
  161.     err = FSRead( refNum, &len, (Ptr) (*qdpic) );
  162.     ToolboxError( "Error loading PICT file, can't read data", err );
  163.     err = FSClose( refNum );
  164.     ToolboxError( "Error loading PICT file, can't close file", err );
  165.     err = NewGWorld( &mygw, 32, &((*qdpic)->picFrame), NULL, NULL, 0 );
  166.     if (err)
  167.         MemoryError( "Error loading PICT file, not enough memory" );
  168.     if (! LockPixels( mygw->portPixMap ) )
  169.         MemoryError( "Can't lock pixels in memory" );
  170.     GetGWorld( &savePort, &saveDevice );
  171.     SetGWorld( mygw, NULL );
  172.     EraseRect( &(mygw->portRect) );    
  173.     DrawPicture( qdpic, &((*qdpic)->picFrame) );
  174.     UnlockPixels( mygw->portPixMap );
  175.     HUnlock( qdpic );
  176.     DisposHandle( qdpic );
  177.     SetGWorld( savePort, saveDevice );
  178.     PtoCstr( name );        /* Leave name as we found it… */
  179.     return( mygw );    
  180. }
  181.  
  182. /*
  183.  * Save a GWorld out to a Pict
  184.  */
  185. void SavePicture( gw, name )
  186. GWorldPtr gw;
  187. char * name;
  188. {
  189.     GDHandle saveDevice;
  190.     CGrafPtr savePort;
  191.     GWorldPtr tmpGW;
  192.     QDErr err;
  193.     
  194.     /*
  195.      * Creating the quickdraw picture requires making a copy
  196.      * of the picture in memory.  This pre-flights the operation
  197.      * so we know if it can succede
  198.      */
  199.      
  200.     err = NewGWorld( &tmpGW, (*(gw->portPixMap))->pixelSize, &gw->portRect, NULL, NULL, 0 );
  201.     if (err)
  202.         MemoryError( "Not enough memory to make a PICT file" );
  203.     else
  204.         DisposeGWorld( tmpGW );
  205.  
  206.     GetGWorld( &savePort, &saveDevice );
  207.     SetGWorld( (CGrafPtr) gw, NULL );
  208.     if (! LockPixels( gw->portPixMap ) )
  209.         MemoryError( "Can't lock pixels in memory" );
  210.     thePic = OpenPicture( &gw->portRect );
  211.     CopyBits( *gw->portPixMap, *gw->portPixMap, 
  212.                 &gw->portRect, &gw->portRect, srcCopy, NULL );
  213.     WritePictFile( name );
  214.     UnlockPixels( gw->portPixMap );
  215.     SetGWorld( savePort, saveDevice );
  216. }    
  217.  
  218.  
  219. /* 
  220.  * Copy an Offscreen gworld to an onscreen window
  221.  */
  222. void CopyGWtoWindow( gw, window )
  223. GWorldPtr gw;
  224. WindowPtr window;
  225. {
  226.     RGBColor savefg, savebg, c;
  227.     Rect srcRect, dstRect;
  228.     GrafPtr savePort;
  229.     
  230.     GetForeColor( &savefg );        /* Robert sez ya gotta do this */
  231.     GetBackColor( &savebg );
  232.     c.red = c.green = c.blue = 0;
  233.     RGBForeColor( &c );
  234.     c.red = c.green = c.blue = 0xFFFF;
  235.     RGBBackColor( &c );
  236.     GetPort( &savePort );
  237.     SetPort( window );
  238.  
  239.     srcRect = (*(gw->portPixMap))->bounds;
  240.     dstRect = window->portRect;
  241.  
  242.     if ( LockPixels( gw->portPixMap ))
  243.     {
  244.         CopyBits( *gw->portPixMap, (BitMap *) &(window->portBits), 
  245.                   &srcRect, &dstRect, srcDither, NULL );
  246.     }
  247.      UnlockPixels( gw->portPixMap );
  248.      SetPort( savePort );
  249.      
  250.     RGBForeColor( &savefg );
  251.     RGBBackColor( &savebg );
  252. }
  253.